Public Class frmComputerHierarchy

    Private _dsItems As TargetComputers
    Private _groupingAttribute As String

    Private Sub frmHierarchy_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        _dsItems = New TargetComputers

        BuildDefaultView()

    End Sub

    Private Sub TreeView1_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick

        If e.Node.Tag.ToString.StartsWith("Group") Then
            Dim GroupValue As String = e.Node.Tag.ToString.Replace("Group-", "")
            GroupValue = GroupValue.Replace("<Not Set>", "")

            Dim dv As DataView = New DataView(Me._dsItems.Tables("computers"))

            Dim rowFilter As String = "ISNULL(@GroupingAttribute,'') = '@GroupValue'"
            rowFilter = rowFilter.Replace("@GroupingAttribute", _groupingAttribute)
            rowFilter = rowFilter.Replace("@GroupValue", GroupValue)

            dv.RowFilter = rowFilter
            dv.Sort = "cn"

            DataGridView1.DataSource = dv

            Me.MainPaneStatusLabel.Text = DataGridView1.RowCount & " computers"

        End If

    End Sub

    Private Sub ToolStripComboBoxGroupBy_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolStripComboBoxGroupBy.SelectedIndexChanged

        Cursor.Current = Cursors.WaitCursor

        'clear the data grid
        DataGridView1.DataSource = Nothing

        _groupingAttribute = Me.ToolStripComboBoxGroupBy.Text

        If _groupingAttribute <> "" Then
            BuildGroupedView(_groupingAttribute)
        Else
            BuildDefaultView()
        End If

        Cursor.Current = Cursors.Default

    End Sub

    Private Sub ToolStripRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripRefresh.Click

        Cursor.Current = Cursors.WaitCursor

        _dsItems.Refresh()

        _groupingAttribute = Me.ToolStripComboBoxGroupBy.Text

        If _groupingAttribute <> "" Then
            BuildGroupedView(_groupingAttribute)
        Else
            BuildDefaultView()
        End If

        Cursor.Current = Cursors.Default

    End Sub

#Region "Core Functions"

    Private Sub BuildDefaultView()

        'load the initial data grid with all target accounts
        Dim dv As DataView = New DataView(_dsItems.Tables("computers"))
        dv.Sort = "cn"

        DataGridView1.DataSource = dv

        Me.MainPaneStatusLabel.Text = DataGridView1.RowCount & " computers"

        'build the selection of attributes that can be grouped
        Dim dt As DataTable = dv.ToTable

        Me.ToolStripComboBoxGroupBy.Items.Clear()

        Dim dc As DataColumn
        For Each dc In dt.Columns
            Me.ToolStripComboBoxGroupBy.Items.Add(dc.ColumnName)
        Next

    End Sub

    Private Sub BuildGroupedView(ByVal GroupedAttribute As String)

        'get the dataset...
        _dsItems.SetGrouping(GroupedAttribute)

        Dim dvGrouping As DataView = New DataView(_dsItems.Tables("Grouping1"))
        dvGrouping.Sort = dvGrouping.Table.Columns(0).ColumnName

        TreeView1.Nodes.Clear()

        'loop through the first grouping
        Dim dr As DataRow
        For Each dr In dvGrouping.ToTable.Rows
            Dim value As String
            If Not IsDBNull(dr(0)) Then
                value = dr(0)
            Else
                value = "<Not Set>"
            End If

            Dim mynode As New TreeNode

            With mynode
                .Text = value
                .Tag = "Group-" & value
                .ImageIndex = 0
                .SelectedImageIndex = 1
            End With

            TreeView1.Nodes.Add(mynode)
            Me.ToolStripStatusLabel1.Text = TreeView1.Nodes.Count & " groups"
        Next

        'add details

    End Sub

#End Region


End Class